/****************************************************
	文件：UIManager.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/9/26 1:27:45
	功能：UI管理器
*****************************************************/

using HTUtility;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace RedSpotSystem
{
    /// <summary>
    /// UI Window Message 类型
    /// </summary>
    public enum WndMsgType
    {

    }
    public class UIManager : Singleton<UIManager>
    {
        /// <summary>
        /// 初始化 UIManager
        /// </summary>
        /// <param name="uiRoot">UI根节点</param>
        /// <param name="wndRoot">窗口根节点</param>
        /// <param name="eventSystem">事件中心</param>
        /// <param name="uiCamera">UI相机</param>
        public void Init(RectTransform uiRoot, RectTransform wndRoot, EventSystem eventSystem, Camera uiCamera)
        {
            Debug.Log("UIManager init done.");
        }

        /// <summary>
        /// 初始化 UI Components （递归搜索所有符合命名规范的UI子物体，存入Dict中）
        /// </summary>
        /// <param name="comps">存储UI组件的字典</param>
        /// <param name="obj">当前GameObject</param>
        public void InitUIComponents(ref Dictionary<string, UnityEngine.Object> comps, GameObject obj)
        {
            if (obj == null) return;
            if (obj.transform.childCount > 0)
            {
                string name = string.Empty;
                string headStr = string.Empty;
                GameObject curGo = null;
                for (int i = 0; i < obj.transform.childCount; i++)
                {
                    curGo = obj.transform.GetChild(i).gameObject;
                    name = curGo.name;
                    if (name.Contains("_") == false) continue;
                    headStr = name.Split('_')[0];
                    switch (headStr)
                    {
                        case "Obj":
                            comps.Add(name, curGo);
                            break;
                        case "Trans":
                            comps.Add(name, curGo.transform);
                            break;
                        case "Txt":
                            comps.Add(name, curGo.GetComponent<Text>());
                            break;
                        case "Img":
                            comps.Add(name, curGo.GetComponent<Image>());
                            break;
                        case "RImg":
                            comps.Add(name, curGo.GetComponent<RawImage>());
                            break;
                        case "Btn":
                            comps.Add(name, curGo.GetComponent<Button>());
                            break;
                        case "Tog":
                            comps.Add(name, curGo.GetComponent<Toggle>());
                            break;
                        case "Sli":
                            comps.Add(name, curGo.GetComponent<Slider>());
                            break;
                        case "Scrbar":
                            comps.Add(name, curGo.GetComponent<Scrollbar>());
                            break;
                        case "Dropdown":
                            comps.Add(name, curGo.GetComponent<Dropdown>());
                            break;
                        case "InputField":
                            comps.Add(name, curGo.GetComponent<InputField>());
                            break;
                        case "ScrView":
                            comps.Add(name, curGo.GetComponent<ScrollRect>());
                            break;
                        case "Cam":
                            comps.Add(name, curGo.GetComponent<Camera>());
                            break;
                        case "Col":
                            comps.Add(name, curGo.GetComponent<BoxCollider>());
                            break;
                        default:
                            Debug.LogErrorFormat("尚未定义的UIComps前缀：{0}", headStr);
                            break;
                    }
                    InitUIComponents(ref comps, curGo);
                }
            }
        }
    }
}
